<template>
  <div>
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body :before-close="close">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-row>
          <el-col :span="8">
            <el-form-item label="客户代码" prop="account_code">
              <el-input v-model="form.account_code" @input="changeUpper" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户名称" prop="account_name">
              <el-input v-model="form.account_name" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户简称" prop="account_key">
              <el-input v-model="form.account_key" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="税号" prop="tax">
              <el-input v-model="form.tax" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="开户行" prop="bank_name">
              <el-input v-model="form.bank_name" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="银行账号" prop="bank_code">
              <el-input v-model="form.bank_code" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="USCI#" prop="usci">
              <el-input v-model="form.usci" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="TAXID" prop="tax_id">
              <el-input v-model="form.tax_id" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="结算方">
              <el-input v-model="form.settlement" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="国家">
              <el-input v-model="form.country" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="省（州）">
              <el-input v-model="form.prov" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="城市">
              <el-input v-model="form.city" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="邮编">
              <el-input v-model="form.zip_code" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系人" prop="relation_name">
              <el-input v-model="form.relation_name" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系电话" prop="relation_phone">
              <el-input v-model="form.relation_phone" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="地址" prop="addr">
              <el-input v-model="form.addr" type="textarea" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="角色" prop="role">
              <el-checkbox-group v-model="role">
                <el-checkbox v-for="(item,index) in data" :key="index" :label="item.id" border>{{ item.part_name }}</el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row style="top: 30px;margin-bottom: 30px;text-align:center;">
          <el-col :span="24">
            <el-button type="primary" size="medium" @click="submitForm('form')">{{ title }}</el-button>
            <el-button size="medium" @click="close()">取消</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { providerAdd, providerEdit, partList } from '@/api/basics'
export default {
  name: 'AddEdit',
  props: {
    open: {
      type: Boolean
    },
    form: {
      type: Object
    },
    title: {
      type: String
    }
  },
  data() {
    return {
      rules: {
        account_code: [
          { required: true, message: '请填写账户代码', trigger: 'blur' },
          { max: 16, message: '账户代码最长16位字符', trigger: 'change' }
        ],
        account_name: [
          { required: true, message: '请填写账户名称', trigger: 'blur' }
        ],
        account_key: [
          { required: true, message: '请填写账户简称', trigger: 'blur' }
        ],
        relation_name: [
          { required: true, message: '请填写联系人', trigger: 'blur' }
        ],
        relation_phone: [
          { required: true, message: '请填写联系电话', trigger: 'blur' }
        ]
      },
      role: [],
      data: []
    }
  },
  watch: {
    'open': function(newValue, oldValue) {
      if (newValue === true && this.title === '编辑') {
        this.role = this.form.role
      } else {
        this.role = []
      }
    }
  },
  created() {
    this.getPartList()
  },
  methods: {
    changeUpper() {
      this.form.account_code = this.form.account_code.toUpperCase()
    },
    getPartList() {
      partList().then(response => {
        if (response.code === 10000) {
          this.data = response.data
        }
      })
    },
    submitForm(formName) {
      const that = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (that.title === '添加') {
            this.add()
          } else {
            this.edit()
          }
        } else {
          return false
        }
      })
    },
    add() {
      const that = this
      if (that.role.length === 0) {
        that.$message.error('至少选择一个角色')
        return false
      }
      that.form.role = that.role.toString()
      // console.log(that.form.role)
      // return
      providerAdd(that.form).then(response => {
        if (response.code === 10000) {
          that.$message.success(response.message)
          that.$parent.getList()
          that.$emit('update:open', false)
        }
      })
    },
    edit() {
      const that = this
      if (that.role.length === 0) {
        that.$message.error('至少选择一个角色')
        return false
      }
      that.form.role = that.role.toString()
      providerEdit(that.form).then(response => {
        if (response.code === 10000) {
          that.$message.success(response.message)
          that.$parent.getList()
          that.$emit('update:open', false)
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    close() {
      this.$emit('update:open', false)
    }
  }
}
</script>

